package com.agent.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.agent.entity.RepaireInfo;
import com.agent.page.EasyuiPage;
import com.agent.util.Constants;
import com.agent.vo.RepaireCycle;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.agent.repository.RepaireAllocationMapper;
import com.agent.entity.RepaireAllocation;
import com.agent.service.RepaireAllocationService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@Service("RepaireAllocationService")
public class RepaireAllocationServiceImpl implements RepaireAllocationService{
	
	@Autowired
	private RepaireAllocationMapper RepaireAllocationMapper;

	public void delete(String id) {
		RepaireAllocationMapper.delete(id);		
	}

	@Override
	public void insert(RepaireInfo repaireInfo, String type) {
		RepaireAllocation repaireAllocation = new RepaireAllocation();
		repaireAllocation.setrepaireId(repaireInfo.getId());
		repaireAllocation.setengineer(repaireInfo.getCurrentId());
		repaireAllocation.setengineerName(repaireInfo.getCurrentName());
		repaireAllocation.setcreateTime(new Date());
		repaireAllocation.setcreatorName(repaireInfo.getAllocationer());
		repaireAllocation.setType(type);
		RepaireAllocationMapper.insert(repaireAllocation);
	}

	@Override
	public EasyuiPage<RepaireCycle> searchCycle(Integer page, Integer rows,Map searchMap) {
		PageHelper.startPage(page, rows, true, true, true);
		List<RepaireCycle> repaireCycles = searchCycle(searchMap);
		EasyuiPage<RepaireCycle> pages = new EasyuiPage<RepaireCycle>();
		pages.setRows(repaireCycles);
		PageInfo<RepaireCycle> pageinfo = new PageInfo<RepaireCycle>(repaireCycles);
		pages.setTotal(pageinfo.getTotal());
		return pages;
	}

	@Override
	public List<RepaireCycle> searchCycle(Map searchMap) {
		List<RepaireCycle> repaireCycles = RepaireAllocationMapper.searchCycle(searchMap);
		if(!CollectionUtils.isEmpty(repaireCycles)){
			repaireCycles.forEach(repaireCycle -> {
				if(StringUtils.isEmpty(repaireCycle.getFloor())){
					repaireCycle.setAddress(Constants.getAddress(repaireCycle.getAddress()));
				}else{
					if("04".equals(repaireCycle.getAddress())){  // 其他 地区 不添加楼
						repaireCycle.setAddress(Constants.getAddress(repaireCycle.getAddress()) +"-"+repaireCycle.getFloor());
					}else{
						repaireCycle.setAddress(Constants.getAddress(repaireCycle.getAddress()) +"-"+repaireCycle.getFloor()+"楼");
					}
				}
			});
		}
		return repaireCycles;
	}

	public List<RepaireAllocation> getRepaireAllocation(String id) {
		return RepaireAllocationMapper.getRepaireAllocation(id);
	}

	public void insert(RepaireAllocation RepaireAllocation) {
		 RepaireAllocationMapper.insert(RepaireAllocation);
	}

	public List<RepaireAllocation> searchAll() {
		return RepaireAllocationMapper.searchAll();
	}

	public List<RepaireAllocation> searchRepaireAllocation(Map searchMap) {
		return RepaireAllocationMapper.searchRepaireAllocation(searchMap);
	}

	public void update(RepaireAllocation RepaireAllocation) {
		RepaireAllocationMapper.update(RepaireAllocation);
	}

	public RepaireAllocationMapper getRepaireAllocationMapper() {
		return RepaireAllocationMapper;
	}

	public void setRepaireAllocationMapper(RepaireAllocationMapper RepaireAllocationMapper) {
		this.RepaireAllocationMapper = RepaireAllocationMapper;
	}
	
	
	

}
